package com.gogowan.agroenterprise.utils;

import android.app.ActionBar;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;


import com.gogowan.agroenterprise.R;

import java.util.Calendar;

public class DatePickerWindow {
    private static DatePickerWindow window;
    private Activity activity;
    private PopupWindowUtil util;
    private TextView textView;
    private String StartTime="",EndTime="";
    public DatePickerWindow(Activity activity, TextView textView){
        this.activity=activity;
        this.textView=textView;
        util=new PopupWindowUtil(this.activity);
    }
    public static DatePickerWindow init(Activity activity, TextView textView){
        window=new DatePickerWindow(activity,textView);
        return window;
    }
    public void   StartTime(final String  EndTime){
        util.setContextView(R.layout.datepickerwindow_layout).setType( ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        util.getView().findViewById(R.id.peopleback_LL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.getPopupWindow().dismiss();
            }
        });
        util.getView().findViewById(R.id.qx_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.getPopupWindow().dismiss();
            }
        });

        util.getView().findViewById(R.id.WindowLL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        final DatePicker picker=(DatePicker)util.getView().findViewById(R.id.dateSelect_Window_Dp);
        Calendar calendar = Calendar.getInstance();
        final int year=calendar.get(Calendar.YEAR);
        final int monthOfYear=calendar.get(Calendar.MONTH);
        final int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        picker.init(year,monthOfYear,dayOfMonth,dateChangedListener);
        util.show(Gravity.BOTTOM,0,0);
        this.EndTime=EndTime;
        util.getView().findViewById(R.id.qd_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getStr(picker.getYear(),picker.getMonth(),picker.getDayOfMonth());
                util.getPopupWindow().dismiss();
            }
        });
    }

    private void getStr(int  year,int monthOfYear,int  dayOfMonth) {
        String selectDate=year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日";
        String DateStr=Utils.getTime();
        if (StartTime.length()>0&& EndTime.length()>0){
            if (Utils.isDateOneBigger(EndTime,StartTime)){
                Utils.showShortToast(activity,"结束时间不能小于开始时间");
                return;
            }
        }else if (StartTime.length()>0&&!(StartTime.equals(selectDate))){
            if (Utils.isDateOneBigger(selectDate,StartTime)){
                Utils.showShortToast(activity,"结束时间不能小于开始时间");
                return;
            }
        }else if (EndTime.length()>0&&!(EndTime.equals(selectDate))){
            if (Utils.isDateOneBigger(EndTime,selectDate)){
                Utils.showShortToast(activity,"结束时间不能小于开始时间");
                return;
            }
        }
        if (Utils.isDateOneBigger(selectDate,DateStr)){
            textView.setText(selectDate);
//            util.getPopupWindow().dismiss();
        }else {
            Utils.showShortToast(activity,"请选择 "+DateStr+"当天或之前的日期");
        }
    }

    public void   EndTime(String  StatrTime){
        util.setContextView(R.layout.datepickerwindow_layout).setType( ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        util.getView().findViewById(R.id.peopleback_LL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                util.getPopupWindow().dismiss();
            }
        });
        final DatePicker picker=(DatePicker)util.getView().findViewById(R.id.dateSelect_Window_Dp);
        Calendar calendar = Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int monthOfYear=calendar.get(Calendar.MONTH);
        int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        picker.init(year,monthOfYear,dayOfMonth,dateChangedListener);
        util.show(Gravity.BOTTOM,0,0);
        this.StartTime=StatrTime;
        util.getView().findViewById(R.id.qx_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.getPopupWindow().dismiss();
            }
        });
        util.getView().findViewById(R.id.qd_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getStr(picker.getYear(),picker.getMonth(),picker.getDayOfMonth());
                util.getPopupWindow().dismiss();
            }
        });
    }
    private DatePicker.OnDateChangedListener dateChangedListener=new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            getStr(year,monthOfYear,dayOfMonth);
//            String selectDate=year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日";
//            String DateStr=Utils.getTime();
//            if (StartTime.length()>0&& EndTime.length()>0){
//                if (Utils.isDateOneBigger(EndTime,StartTime)){
//                    Utils.showShortToast(activity,"结束时间不能小于开始时间");
//                    return;
//                }
//            }else if (StartTime.length()>0&&!(StartTime.equals(selectDate))){
//                if (Utils.isDateOneBigger(selectDate,StartTime)){
//                    Utils.showShortToast(activity,"结束时间不能小于开始时间");
//                    return;
//                }
//            }else if (EndTime.length()>0&&!(EndTime.equals(selectDate))){
//                if (Utils.isDateOneBigger(EndTime,selectDate)){
//                    Utils.showShortToast(activity,"结束时间不能小于开始时间");
//                    return;
//                }
//            }
//            if (Utils.isDateOneBigger(selectDate,DateStr)){
//                textView.setText(selectDate);
//                util.getPopupWindow().dismiss();
//            }else {
//                Utils.showShortToast(activity,"请选择 "+DateStr+"当天或之前的日期");
//            }
        }
    };



}
